rm(list=ls())
load("../Data/revealed.RData")
set.seed(873209)
adapt <- 1e4
burn <- 25e4
samp <- 1e4
thin <- 25

allmeps$nationalparty <- ifelse(is.na(allmeps$nationalparty),"missing",allmeps$nationalparty)
group.counter <- unique(allmeps$nationalparty)
group <- rep(NA,nrow(allmeps))
for (i in 1:length(group.counter)){
  group<- ifelse(allmeps$nationalparty==group.counter[i],i,group)
}
n.groups <- length(levels(as.factor(allmeps$nationalparty)))

year <- allmeps$ep -3
n.year <- length(unique(year))

chains <- 3

allmeps$nationalopen <- ifelse(is.na(allmeps$nationalopen),0,allmeps$nationalopen)
allmeps$nationalclosed <- ifelse(is.na(allmeps$nationalclosed),0,allmeps$nationalclosed)
allmeps$euopen <- ifelse(is.na(allmeps$euopen),0,allmeps$euopen)
allmeps$natback <- ifelse(is.na(allmeps$natback),0,allmeps$natback)
allmeps$other <- ifelse(is.na(allmeps$other),0,allmeps$other)
allmeps$epback <- ifelse(is.na(allmeps$epback),0,allmeps$epback)
allmeps$groupRole <- ifelse(is.na(allmeps$groupRole),0,allmeps$groupRole)
allmeps$committeeRole <- ifelse(is.na(allmeps$committeeRole),0,allmeps$committeeRole)

X <- with(allmeps,cbind(nationalopen,nationalclosed,euopen,epback,natback,other,age,groupRole,committeeRole))
n.betas <- ncol(X)
Jagsdata <- list(r=allmeps$votes,n=allmeps$totalvotes,n.obs=nrow(allmeps),
                 X=X,group=group,n.groups=n.groups,
                 year=year,n.year=n.year,n.betas=n.betas,
                 b0=rep(0,n.betas),B0=diag(.001,n.betas))


initsfunction <- function(chain) return(switch(chain,
                                               "1"=list(deltaTmp=rnorm(n.groups,0,2),beta=rnorm(n.betas,0,3),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Super-Duper",.RNG.seed=2), 
                                               "2"=list(deltaTmp=rnorm(n.groups,-1,3),beta=rnorm(n.betas,-1,2),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Wichmann-Hill",.RNG.seed=2),
                                               "3"=list(deltaTmp=rnorm(n.groups,1,2),beta=rnorm(n.betas,1,1.5),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Mersenne-Twister",.RNG.seed=2)))

foo <- run.jags(model="../jags/binomialhierNat.jags",
                data=Jagsdata,inits=initsfunction,modules="glm",
                adapt=adapt,burnin=burn,sample=samp,
                n.chains=chains,method='parallel',thin=thin,#summarise=FALSE,plots=FALSE,
                monitor=c("beta","deviance","nu"))
resVotes <- as.mcmc.list(foo)
save(resVotes,file="../Posteriors/ResultsVotesNat.RData")

# Debates $ only for EP5 - EP7
debmeps <- subset(allmeps,subset=allmeps$ep>4) 
group.counter <- unique(debmeps$nationalparty)
group <- rep(NA,nrow(debmeps))
for (i in 1:length(group.counter)){
  group<- ifelse(debmeps$nationalparty==group.counter[i],i,group)
}
n.groups <- length(levels(as.factor(debmeps$nationalparty)))


nat.counter <- unique(debmeps$state)
nat <- rep(NA,nrow(debmeps))
for (i in 1:length(nat.counter)){
  nat<- ifelse(debmeps$state==nat.counter[i],i,nat)
}
n.nat <- length(levels(as.factor(debmeps$state)))

year <- debmeps$ep -4
n.year <- length(unique(year))

chains <- 3

X <- with(debmeps,cbind(nationalopen,nationalclosed,euopen,epback,natback,other,age,groupRole,committeeRole))
n.betas <- ncol(X)
Jagsdata <- list(r=debmeps$speeches,n=debmeps$debates,n.obs=nrow(debmeps),
                 X=X,group=group,n.groups=n.groups,
                 year=year,n.year=n.year,n.betas=n.betas,
                 b0=rep(0,n.betas),B0=diag(.001,n.betas))

initsfunction <- function(chain) return(switch(chain,
                                               "1"=list(deltaTmp=rnorm(n.groups,0,2),beta=rnorm(n.betas,0,3),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Super-Duper",.RNG.seed=2), 
                                               "2"=list(deltaTmp=rnorm(n.groups,-1,3),beta=rnorm(n.betas,-1,2),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Wichmann-Hill",.RNG.seed=2),
                                               "3"=list(deltaTmp=rnorm(n.groups,1,2),beta=rnorm(n.betas,1,1.5),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Mersenne-Twister",.RNG.seed=2)))

fooDebates <- run.jags(model="../jags/binomialhierNat.jags",
                       data=Jagsdata,inits=initsfunction,modules="glm",
                       adapt=adapt,burnin=burn,sample=samp,
                       n.chains=chains,method='parallel',thin=thin,#summarise=FALSE,plots=FALSE,
                       monitor=c("beta","deviance","nu"))
resDebates <- as.mcmc.list(fooDebates)
save(resDebates,file="../Posteriors/ResultsDebatesNat.RData")

